package com.lagou.edu.controller;


import cn.hutool.core.date.DateUtil;
import com.lagou.edu.bean.MailReturn;
import com.lagou.edu.bean.ToEmail;
import com.lagou.edu.service.SendEmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 *  发送验证码到邮箱，true成功，false失败
 *  /email/{email}/{code}
 * </p>
 *
 * @author jobob
 * @since 2021-03-19
 */
@RestController
@RequestMapping("/email")
@Slf4j
public class EmailController {

    @Autowired
    private SendEmailService sendEmailService;

    @GetMapping("/send/{email}/{code}")
    public MailReturn sendEmail(@PathVariable("email")String email, @PathVariable("code")String code){
        ToEmail toEmail = new ToEmail();
        toEmail.setTos(new String[]{email});
        toEmail.setSubject("注册验证码（请注意失效时间！！！）");
        toEmail.setContent("验证码：" + code);

        log.info("发送邮件开始时间：" + DateUtil.date());
        MailReturn mailReturn = sendEmailService.commonEmail(toEmail);
        log.info(mailReturn.getMessage() + ":" + toEmail.toString());
        return mailReturn;
    }
}
